<?php

namespace app\api\model;

use app\api\model\ConfigSystem as Config;
use think\Db;

class MallDistribution extends Common {
    protected $connection = 'database';

    /**
     * @param $oredr_id
     * @return int
     */
    public static function Add($oredr_id) {
        $order = MallOrder::get($oredr_id);
        if (!$order) {
            return 10000;
        }
        if ($order['status'] != 1) {
            return 10001;
        }
        if ($order['total'] <= 0) {
            return 10002;
        }
        $config      = Config::index('invite');
        $userModel   = new User();
        $uid = $order->uid;
        $user_channel_profile = Db::connect(getenv('APP_MALL_DATABASE'))->name('user_channel_profile')->where(['user_id'=>$uid, 'sys_channel'=>getenv('APP_CHANNEL')])->find();
        if ($user_channel_profile) {
            $uid = $user_channel_profile['source_uid'] ?: $uid;
        }

        $user        = $userModel->find($uid);
        $one         = false;
        $two         = false;
        $three       = false;
        $totalPrice  = $order['total'] * 5;
        $one_rate    = $config['shouyi_mall_1'];
        $two_rate    = $config['shouyi_mall_2'];
        $three_rate  = $config['shouyi_mall_3'];
        $one_price   = $one_rate * $totalPrice;
        $two_price   = $two_rate * $totalPrice;
        $three_price = $three_rate * $totalPrice;
        $insert      = [
            'user_id'          => $order->uid,
            'order_id'         => $oredr_id,
            'totalprice'       => $totalPrice,
            'one_uid'          => 0,
            'one_rate'         => 0,
            'one_commission'   => 0,
            'two_uid'          => 0,
            'two_rate'         => 0,
            'two_commission'   => 0,
            'three_uid'        => 0,
            'three_rate'       => 0,
            'three_commission' => 0,
            'status'           => 0,
            'create_time'      => time(),
        ];
        if ($user['yqr']) {
            $one = $userModel->where(['yqm' => $user['yqr']])->field('id,yqr')->find();
            if (!$one) {
                return 10003;
            }
            $one_o = _channelUser($one['id']);

            $fanli_inserts = [];
            $timenow = time();

            $insert['one_uid']        = $one_o['id'];
            $insert['one_rate']       = $one_rate;
            $insert['one_commission'] = $one_price;

            $fanli_inserts[] = [
                'user_id'=>$one_o['id'],
                'order_id'=>$oredr_id,
                'fanli_count'=>$one_price,
                'fanli_day'=>365,
                'fanli_day_rate'=>1 / 365 * 100,
                'create_time'=>$timenow,
            ];

            if ($one['yqr']) {
                $two = $userModel->where(['yqm' => $one['yqr']])->field('id,yqr')->find();
                if ($two) {
                    $two_o = _channelUser($two['id']);
                    $insert['two_uid']        = $two_o['id'];
                    $insert['two_rate']       = $two_rate;
                    $insert['two_commission'] = $two_price;

                    $fanli_inserts[] = [
                        'user_id'=>$two_o['id'],
                        'order_id'=>$oredr_id,
                        'fanli_count'=>$two_price,
                        'fanli_day'=>365,
                        'fanli_day_rate'=>1 / 365 * 100,
                        'create_time'=>$timenow,
                    ];

                    if ($two['yqr']) {
                        $three = $userModel->where(['yqm' => $two['yqr']])->field('id,yqr')->find();
                        if ($three) {
                            $three_o = _channelUser($three['id']);
                            $insert['three_uid']        = $three_o['id'];
                            $insert['three_rate']       = $three_rate;
                            $insert['three_commission'] = $three_price;

                            $fanli_inserts[] = [
                                'user_id'=>$three_o['id'],
                                'order_id'=>$oredr_id,
                                'fanli_count'=>$three_price,
                                'fanli_day'=>365,
                                'fanli_day_rate'=>1 / 365 * 100,
                                'create_time'=>$timenow,
                            ];
                        }
                    }
                }
            }

            try {
                self::create($insert);
                if (sizeof($fanli_inserts)) {
                    Db::connect(getenv('APP_MALL_DATABASE'))->name('mall_order_fanli')->insertAll($fanli_inserts);
                }
            } catch (\Exception $e) {
                return $e->getMessage();
            }
        }
    }

    public static function setStatus($oredr_id, $status = 1)
    {
        $distribution = self::get(['order_id'=>$oredr_id]);
        if (!$distribution) {
            return;
        }
        $distribution->status = $status;
        $distribution->update_time = time();
        $distribution->save();
    }
}
